<?php
namespace App\Logics;

use App\Models\AdminOperationLog as ModelAdminOperationLog;
use App\Models\AdminUser;
use Dcat\Admin\Admin;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class AdminOperationLogLogic extends Logic
{

    protected $data;
    protected $type;
    protected $model;
    protected $description = [];
    protected $detail=[];
    protected $relation_id;
    protected $relation_table;

    /**
     * Undocumented function
     *
     * @param Model|null $model
     */
    public function __construct(Model $model = null, $admin_user = null)
    {
        $this->model = $model;
        if (!$admin_user) {
            $admin_user = $this->getAdminUser();
        }
        parent::__construct($admin_user);
    }

    /**
     * 手动设置操作人员
     *
     * @param AdminUser $admin_user
     * @return $this
     */
    public function admin_user(AdminUser $admin_user)
    {
        $this->admin_user = $admin_user;
        return $this;
    }

    /**
     * 登录日志
     * @return $this
     */
    public function login()
    {
        $this->type = ModelAdminOperationLog::TYPE_LOGIN;
        $this->description = ['登录成功'];
        return $this;
    }

    /**
     * 设置登录类型
     *
     * @param string $type
     * @return $this
     */
    public function type($type = ModelAdminOperationLog::TYPE_OTHER)
    {
        $this->type = $type;
        return $this;
    }

    public function update($description = '')
    {
        $this->type = ModelAdminOperationLog::TYPE_EDIT;
        if ($description) {
            $this->description($description);
        }
        return $this;
    }

    public function create($description = '')
    {
        $this->type = ModelAdminOperationLog::TYPE_CREATE;
        $this->description($description);
        return $this;
    }

    public function delete($description = '')
    {
        $this->type = ModelAdminOperationLog::TYPE_DELETE;
        $this->description($description);
        return $this;
    }

    /**
     * 设置描述文字
     *
     * @param string|array $description
     * @return $this
     */
    public function description($description = '')
    {
        if (is_string($description)) {
            array_push($this->description, $description);
        } else if (is_array($description)) {
            $this->description = $description;
        }
        return $this;
    }

    /**
     * 设置详细文字
     *
     * @param string|array $detail
     * @return $this
     */
    public function detail($detail = '')
    {
        if (is_string($detail)) {
            array_push($this->detail, $detail);
        } else if (is_array($detail)) {
            $this->detail = $detail;
        }
        return $this;
    }

    public function relation($relation_id = '', $relation_table = '')
    {
        if ($this->model) {
            $this->relation_id = $this->model ? ($this->model->id ?: 0) : 0;
            $this->relation_table = $this->model ? $this->model->getTable() : '';
        } else {
            $this->relation_id = $relation_id;
            $this->relation_table = $relation_table;
        }
    }

    /**
     * 插入新日志
     *
     */
    public function log()
    {
        try {
            ModelAdminOperationLog::create([
                'user_id' => $this->admin_user ? $this->admin_user->id : 0,
                'gid' => $this->admin_user ? $this->admin_user->gid : '',
                'user_name' => $this->admin_user ? $this->admin_user->name : '',
                'path'    => substr(request()->path(), 0, 255),
                'method'  => request()->method(),
                'ip'      => getIp(),
                'input'   => json_encode(request()->input()),
                'description' => is_array($this->description) ? $this->description : [$this->description],
                'relation_table' => $this->relation_table,
                'relation_id' => $this->relation_id,
                'type' => $this->type ?: ModelAdminOperationLog::TYPE_OTHER,
            ]);
        } catch (\Throwable $th) {
            //throw $th;
        }
    }

    
}